package com.spzx.channel.service.impl;

import com.spzx.channel.service.IIndexService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.RemoteCategoryService;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.api.domain.vo.CategoryVo;
import com.spzx.product.api.domain.ProductSku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IIndexService {

    @Autowired
    private RemoteCategoryService remoteCategoryService;

    @Autowired
    private RemoteProductService remoteProductService;

    /**
     * 首页数据
     *
     * @return
     */
    @Override
    public Map<String, Object> index() {
        //获取首页一级分类集合
        R<List<CategoryVo>> oneCategoryListResult = remoteCategoryService.getOneCategory(SecurityConstants.INNER);
        if (R.FAIL == oneCategoryListResult.getCode()) {
            throw new ServiceException(oneCategoryListResult.getMsg());
        }
        List<CategoryVo> categoryList = oneCategoryListResult.getData();

        //获取首页畅销商品集合
        R<List<ProductSku>> topSaleProductSkuListResult = remoteProductService.getTopSale(SecurityConstants.INNER);
        if (R.FAIL == topSaleProductSkuListResult.getCode()) {
            throw new ServiceException(topSaleProductSkuListResult.getMsg());
        }
        List<ProductSku> productSkuList = topSaleProductSkuListResult.getData();

        //封装成Map
        HashMap<String, Object> mapData = new HashMap<>();
        mapData.put("categoryList", categoryList);
        mapData.put("productSkuList", productSkuList);

        //返回数据
        return mapData;
    }
}
